<?php

//最终快照,302递归
function finalsnapshot($url,$_Method = "GET", $_Form_Data = "", $_Cookie = "", $_Headers = array()){
    
    $content = Get_Web_Contents($url,$_Method, $_Form_Data, $_Cookie, $_Headers);
    
    if(empty($content['Info']['http_code'])) return ;
    
    $content['Info']['referrer'] = $url;
    
    if($content['Info']['http_code']=='302'||$content['Info']['http_code']=='301'){
        return finalsnapshot($content['Info']['redirect_url'],$_Method, $_Form_Data, $_Cookie, $_Headers);
    }else{
        return $content;
    }
}
//请求头
function getheader($host = 'www.baidu.com'){
    return array(
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language: zh-CN,zh;q=0.8",
        "Content-Encoding: gzip",
        "Cache-Control: max-age=0",
        "Upgrade-Insecure-Requests: 1",
        "Host:$host",
    );
}
//请求头(移动)
function getmobileheader($host = 'm.baidu.com'){
    return array(
        'User-Agent' => "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
        "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language: zh-CN,zh;q=0.8",
        "Content-Encoding: gzip",
        "Cache-Control: max-age=0",
        "Upgrade-Insecure-Requests: 1",
        "Host:$host",
    );
}
/**
 * 等待其他进程结束
 * @param  [type] $Str [description]
 * @return [type]      [description]
 */
function status($filepath){
    if(!file_exists($filepath)){
        file_put_contents($filepath,"Run");
    }else{
        //等待其他进程结束
        while(file_get_contents($filepath)!="End"){
            file_put_contents($filepath,"Exit");sleep(1);
        }
        file_put_contents($filepath,"Run");
    }
}
/**
 * 命中词加背景
 * @param  [type] $Str [description]
 * @return [type]      [description]
 */
function codeWords($code) {
    Global $keywords;
    Global $brand;
    
    //去css,js,a
    $code = strip_tags(preg_replace("#<(style|script|a)[ |>](.*?)</(style|script|a)>#i", "", $code));
    
    if(!empty($brand)){
        $brand2 = $brand;
        
        array_walk($brand2, 'addSpan');
        
        $code = str_replace($brand, $brand2, $code,$count);
    }else{
        $count = 1;
    }
    
    if(!empty($count)){
    
        $keywords2 = $keywords;
        
        array_walk($keywords2, 'addSpan');
        
        $code = str_replace($keywords, $keywords2, $code,$count);
        
        if($count > 1){//命中数
            return array('snapshot'=>$code,'count'=>$count);
        }
        
    }
    return false;
}
//命中词加背景
function addSpan(&$value, $key) {
    $value = "<span style='background-color: #ffff66;'>" . $value . "</span>";
}
/**
 * 判断环境类型编码输出
 * @param  [type] $Str [description]
 * @return [type]      [description]
 */
function print_f($Str, $sleep = null)
{
    // 判断是否为 cli
    if(php_sapi_name() == 'cli'){
        // 如果是 windows 系统则转换编码为 gbk
        if(stripos(php_uname(), "win") !== false){
            $Str = iconv('utf-8', 'gb2312', $Str);
        }
        // 是则转换编码为 gbk
        echo $Str;
        // 参数二是数字就延迟
        is_numeric($sleep) && sleep($sleep);
    }else{
        // 正常输出
        echo $Str;
    }
    // 如果参数二是布尔值 就 判断是否 停止
    if(is_bool($sleep) && $sleep == true) exit;
}
